<?php

namespace app\v1\model;

use think\Model;

/**
 * 用户信息模型类
 */
class User extends Model
{
    /**
     * 用户登录
     */
    public function Login($mobile, $pwd) 
    {
        $user = $this->where('mobile', $mobile)->find();

        if(empty($user) || $user['pwd'] != md5($pwd)) {
            return Result(0, '您的账号或者密码错误');
        }

        $token = (new \RSA\RSA())->Encode($user['id']);
        return Result(1, '登录成功', [
            'token' => $token
        ]);
    }

    /**
     * 注册用户
     */
    public function CreateUser($name, $mobile, $pwd)
    {
        if(!preg_match("/^1[34578]\d{9}$/", $mobile)) { 
            return Result(0, '手机号错误');
        }

        if (empty($pwd) || strlen($pwd) <= 5) {
            return Result(0, '密码长度最少要大于6位');
        }

        if (empty($name)) {
            return Result(0, '昵称不能为空');
        }

        if ($this->where('mobile', $mobile)->count() > 0) {
            return Result(0, '该手机号已存在');
        }

        if ($this->where('name', $name)->count() > 0) {
            return Result(0, '该用户名已存在');
        }
        $id = $this->insertGetId([
            'mobile' => $mobile,
            'pwd' => md5($pwd),
            'name' => $name,
        ]);

        if ($id) {
            //rsa加密 将token传给前端 方便判断用户是否存在
            $token = (new \RSA\RSA())->Encode($id);
            return Result(1, '注册成功', [
                'token' => $token
            ]);
        }

        return Result(0, '注册出现错误');
    }

    /**
     * 修改信息
     */
    public function ModifyUser($user_id = 0, $option)
    {
        if (empty($user_id)) {
            return Result(0, '修改的用户不正确');
        }

        if (!count($option)) {
            return Result(0, '修改的数据不正确');
        }

        $this->where('id', $user_id)->data($option)->update();

        return Result(1, '修改成功');
    }

    /**
     * 获取用户信息
     */
    public function ReadInfo($user_id)
    {
        $user = $this->where('id', $user_id)->find();

        if (!$user) {
            return Result(0, '该用户不存在');
        }

        return Result(1, '', [
            'user' => $user
        ]);
    }

    /**
     * 获取用户的所有订单总数
     */
    public function GetReserveNum($user_id)
    {
        // 创建匿名函数 方便多次调用 用完自动销毁
        $buildDb = function ($state) use ($user_id) {
            return db('rooms_user', [], false)->where('user_id', $user_id)->where('state',$state);
        };
        $reserveMap[0] = $buildDb(0)->count(); // 已完成的订单
        $reserveMap[1] = $buildDb(1)->count(); // 已预定的订单
        $reserveMap[2] = $buildDb(2)->count(); // 正在入住的订单
        return Result(1, '', [
            'reserveNum' => $reserveMap
        ]);
        
    }

    /**
     * 根据指定条件获取用户的订单
     */
    public function GetReserveList($page, $user_id, $state)
    {
        // 创建匿名函数 方便多次调用 用完自动销毁
        $buildDb = function () use ($user_id, $state) {
            $db = db('rooms_user', [], false)
                    ->alias('ru')
                    ->field('ru.*,ri.url,r.name, re.name region_name ,rp.name parent_name')
                    ->join('rooms_img ri', 'ri.rooms_id = ru.rooms_id', 'LEFT')
                    ->join('rooms r', 'r.id=ru.rooms_id', 'LEFT')
                    ->join('region re', 'r.region_id = re.id', 'LEFT')
                    ->join('region rp', 're.parent_id = rp.id', 'LEFT')
                    ->where('ru.user_id', $user_id)->where('ru.state',$state);

            return $db->group('ru.id');
        };

        $count = $buildDb()->count();
        $list = $buildDb()->page($page, 10)->select();

        return Result(1, '', [
            'count' => $count,
            'pageSize' => 10,
            'list' => $list
        ]);
    }
}
